import 'package:flutter/material.dart';
import 'package:flutter_demo/views/login_two/style/theme.dart' as theme;
import 'sign_in.dart';

class LoginPageNew extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LoginPageNewState();
  }
}

class _LoginPageNewState extends State<LoginPageNew> {
  int _currentPage = 0;
  PageController _pageController;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: 0);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          decoration: BoxDecoration(
            gradient: theme.Theme.primaryGradient,
          ),
          child: _buildOuterMostColumn(),
        ),
      ),
    );
  }

  ///最外层的垂直布局
  Widget _buildOuterMostColumn() {
    return Column(
      children: <Widget>[
        SizedBox(height: 75), //顶部的75dp间距
        _buildTopImage(), //顶部的Image
        SizedBox(height: 20),
        _buildIndicatorRow(),
        _buildPageView(),
      ],
    );
  }

  ///顶部图片
  Widget _buildTopImage() {
    return Image(
      width: 250,
      height: 191,
      image: AssetImage("assets/images/login_logo.png"),
    );
  }

  ///创建指示器
  Widget _buildIndicatorRow() {
    return Container(
      width: 300,
      height: 50,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(25)),
        color: Color(0x552b2b2b),
      ),
      child: Row(
        children: <Widget>[
          _buildLeftIndicator(),
          _buildRightIndicator(),
        ],
      ),
    );
  }

  ///创建左边指示器控件
  Widget _buildLeftIndicator() {
    return Expanded(
      flex: 1,
      child: Container(
        decoration: _currentPage == 0
            ? BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(25)),
                color: Colors.white,
              )
            : null,
        child: Center(
          child: FlatButton(
              onPressed: () {
                switchPage(0);
              },
              child: Text(
                "Existing",
                style: TextStyle(fontSize: 16),
              )),
        ),
      ),
    );
  }

  ///创建左边指示器控件
  Widget _buildRightIndicator() {
    return Expanded(
      flex: 1,
      child: Container(
        decoration: _currentPage == 1
            ? BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(25)),
                color: Colors.white,
              )
            : null,
        child: Center(
          child: FlatButton(
              onPressed: () {
                switchPage(1);
              },
              child: Text(
                "New",
                style: TextStyle(fontSize: 16),
              )),
        ),
      ),
    );
  }

  ///创建PageView
  Widget _buildPageView() {
    return Expanded(
      flex: 1,
      child: PageView(
        controller: _pageController,
        children: <Widget>[
          SignInWidget(),
          Text(" Page Two"),
        ],
        onPageChanged: (index) {
          setState(() {
            _currentPage = index;
          });
        },
      ),
    );
  }

  void switchPage(int page) {
    setState(() {
      _currentPage = page;
    });
//    _pageController.jumpToPage(_currentPage);
    _pageController.animateToPage(_currentPage,
        duration: Duration(milliseconds: 500), curve: Curves.decelerate);
  }
}
